package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.adpter.food.DieCollocationDetailMaterialRecyclerAdapter;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XGridView;
import com.yingpai.fitness.dvp_2_0.common.view.statusbar.Status;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomLinearLayoutManager;
import com.yingpai.fitness.entity.food.MaterialBean;
import com.zzhoujay.richtext.RichText;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class DieCollocationDetailActivity extends CommonActivity {


    @BindView(R.id.die_collocation_food_detail_banner)
    ImageView dieCollocationFoodDetailBanner;
    @BindView(R.id.fooddetailNameTv)
    TextView fooddetailNameTv;
    @BindView(R.id.die_collocation_detail_food_rv_list)
    XGridView dieCollocationDetailFoodRvList;
    @BindView(R.id.fooddetailPriceTv)
    TextView fooddetailPriceTv;
    @BindView(R.id.fooddetailVipPriceTv)
    TextView fooddetailVipPriceTv;
    @BindView(R.id.text_num1)
    TextView textNum1;
    @BindView(R.id.text_num2)
    TextView textNum2;
    @BindView(R.id.text_num3)
    TextView textNum3;
    @BindView(R.id.text_num4)
    TextView textNum4;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.diet_collocation_food_step_rich_tv)
    TextView diet_collocation_food_step_rich_tv;
    @BindView(R.id.die_collocation_detail_material_food_brief_rv_list)
    RecyclerView die_collocation_detail_material_food_brief_rv_list;
    @BindView(R.id.nested_scroll_view)
    NestedScrollView nestedScrollView;
    private DecimalFormat df = new DecimalFormat("######.###");

    private DieCollocationDetailMaterialRecyclerAdapter materialRecyclerAdapter;
    private int id;
    private List<MaterialBean.MapData.ComponentListData> componentListData;
    private QuickAdapter<String> quickAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.CONTENT_INVASION_STATUS);
        setContentView(R.layout.die_collocation_detail_activity);
    }

    @Override
    protected void initView() {
        final int startColor = ContextCompat.getColor(this, R.color.colorPrimary);
        final int endColor = ContextCompat.getColor(this, R.color.colorNavigation);

        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int headerHeight = dieCollocationFoodDetailBanner.getHeight();
                int scrollDistance = Math.min(scrollY, headerHeight);
                float fraction = (float) scrollDistance / (float) headerHeight;
                int statusAlpha = (int) ((float) scrollDistance / (float) headerHeight * 255F);

                Status.with(DieCollocationDetailActivity.this).statusBarBackgroundAlpha(statusAlpha);
                setNavigationViewColor(evaluate(fraction, endColor, startColor));
            }
        });

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getInt("id");
        }

        quickAdapter = new QuickAdapter<String>(this, R.layout.die_collocation_child_list_item) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {

                TextView die_collocation_child_tv = helper.getView(R.id.die_collocation_child_tv);

                die_collocation_child_tv.setText(!TextUtils.isEmpty(item) ? item : "");
            }
        };

        dieCollocationDetailFoodRvList.setAdapter(quickAdapter);

        die_collocation_detail_material_food_brief_rv_list.setLayoutManager(new CustomLinearLayoutManager(getApplicationContext()));
        materialRecyclerAdapter = new DieCollocationDetailMaterialRecyclerAdapter(R.layout.die_detail_material_list_item);
        die_collocation_detail_material_food_brief_rv_list.setAdapter(materialRecyclerAdapter);
    }

    @Override
    protected void initData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("id", id);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_FITNESS_FOOD_DETAIL, params, Url.GET_FITNESS_FOOD_DETAIL, new Callback<MaterialBean>() {
            @Override
            public void onSuccess(MaterialBean materialBean) {
                if (materialBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    componentListData = new ArrayList<>();
                    Glide.with(context)
                            .load(materialBean.getMap().getDietDetail().getPhotoUrl())
                            .centerCrop()
                            .into(dieCollocationFoodDetailBanner);

                    fooddetailNameTv.setText(materialBean.getMap().getDietDetail().getDietDetailName());
                    fooddetailPriceTv.setText("￥ " + df.format(materialBean.getMap().getDietDetail().getCommonPrice() / 100));
                    fooddetailVipPriceTv.setText("会员价：￥ " + df.format(materialBean.getMap().getDietDetail().getMemberPrice() / 100));

                    quickAdapter.addAll(Arrays.asList(materialBean.getMap().getDietDetail().getProperties().split(",")));

                    if (materialBean.getMap().getComponentList() != null) {
                        for (int i = 0; i < materialBean.getMap().getComponentList().size(); i++) {
                            if (materialBean.getMap().getComponentList().get(i).getPropertyCategory() == 1) {
                                materialRecyclerAdapter.addData(materialBean.getMap().getComponentList().get(i));
                            } else {
                                componentListData.add(materialBean.getMap().getComponentList().get(i));
                            }
                        }
                    }
                    text1.setText("千卡");
                    textNum1.setText(materialBean.getMap().getDietDetail().getEnergy());

                    switch (componentListData.size()) {
                        case 0:
                            text2.setVisibility(View.GONE);
                            textNum2.setVisibility(View.GONE);
                            text3.setVisibility(View.GONE);
                            textNum3.setVisibility(View.GONE);
                            text4.setVisibility(View.GONE);
                            textNum4.setVisibility(View.GONE);
                            break;

                        case 1:
                            text2.setText(componentListData.get(0).getPropertyName());
                            textNum2.setText(componentListData.get(0).getPropertyAmount());
                            text3.setVisibility(View.GONE);
                            textNum3.setVisibility(View.GONE);
                            text4.setVisibility(View.GONE);
                            textNum4.setVisibility(View.GONE);
                            break;
                        case 2:
                            text2.setText(componentListData.get(0).getPropertyName());
                            textNum2.setText(componentListData.get(0).getPropertyAmount());

                            text3.setText(componentListData.get(1).getPropertyName());
                            textNum3.setText(componentListData.get(1).getPropertyAmount());

                            text4.setVisibility(View.GONE);
                            textNum4.setVisibility(View.GONE);
                            break;
                        case 3:
                            text2.setText(componentListData.get(0).getPropertyName());
                            textNum2.setText(componentListData.get(0).getPropertyAmount());

                            text3.setText(componentListData.get(1).getPropertyName());
                            textNum3.setText(componentListData.get(1).getPropertyAmount());

                            text4.setText(componentListData.get(2).getPropertyName());
                            textNum4.setText(componentListData.get(2).getPropertyAmount());
                            break;
                    }
                    RichText.fromHtml(materialBean.getMap().getDietDetail().getContent()).into(diet_collocation_food_step_rich_tv);
                } else {
                    showToast(materialBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }
}
